package com.seeyon.apps.brwh.abstraction;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.seeyon.ocip.common.utils.IOUtils;

public class MultipartFileUtils {

  public static String calculateMD5(MultipartFile multipartFile) throws IOException, NoSuchAlgorithmException {
    try (InputStream inputStream = multipartFile.getInputStream()) {
      MessageDigest digest = MessageDigest.getInstance("MD5");
      byte[] buffer = new byte[1024];
      int bytesRead;

      while ((bytesRead = inputStream.read(buffer)) != -1) {
        digest.update(buffer, 0, bytesRead);
      }

      // 完成哈希计算并返回MD5的128位二进制数组
      return  Base64.encodeBase64String(digest.digest());
    }
  }

  public static MultipartFile convert(File file) throws IOException {
    // 使用Apache Commons FileUpload库创建FileItem
    FileItemFactory factory = new DiskFileItemFactory();
    String fieldName = "file"; // 这应该是表单字段的名称
    String contentType = "text/plain"; // 根据实际情况设置文件的MIME类型
    boolean isFormField = false; // 因为是文件，所以不是表单字段
    String fileName = new String(file.getName().getBytes("ISO-8859-1"), "utf-8"); // 处理文件名乱码
    FileItem item = factory.createItem(fieldName, contentType, isFormField, fileName);

    try (InputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = item.getOutputStream()) {
      // 将文件内容写入到FileItem的输出流中
      IOUtils.copy(inputStream, outputStream);
    }

    // 使用Apache Commons FileUpload的FileItem包装成MultipartFile
    return new CommonsMultipartFile(item);
  }

  public static String getClientIp(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
      // 多次反向代理后会有多个ip值，第一个ip才是真实ip
      if( ip.indexOf(",")!=-1 ){
        ip = ip.split(",")[0];
      }
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("HTTP_CLIENT_IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("X-Real-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    return ip;
  }
}